package middleware

import (
	"github.com/gin-gonic/gin"
	"wx-uniapp-go/internal/server/echo"
	"wx-uniapp-go/runtime"
	"wx-uniapp-go/third_party/errs"
)

func InnerRequest() gin.HandlerFunc {
	return func(c *gin.Context) {
		allow := runtime.RunMode == runtime.DEV
		ip := c.ClientIP()
		if !allow {
			// 外网同网络、本地允许访问
			if ip == "47.103.153.227" || ip == "::1" || ip == "127.0.0.1" {
				allow = true
			}
			// 内网10.x.x.x允许外网
			prefix := ip[0:3]
			if !allow && len(ip) > 3 && (prefix == "10." || prefix == "172") {
				allow = true
			}
		}
		if allow {
			c.Next()
		} else {
			// echo.Fail(c).Error(exception.Errorf("仅允许内网调用！:%s", ip))
			echo.Fail(c).Error(errs.Errorf(errs.UnknownCode, "仅允许内网调用！", "ip:%s", ip))
			c.Abort()
			return
		}
	}
}
